package cn.sxt.common.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class HttpUtil {
	/**
	 * 模拟 GET 请求
	 * @param urlStr
	 * @param headerMap
	 * @param paramsMap
	 * @return
	 */
	/*使用get方式操作数据，将网页信息拼接成字符串*/
	public String methodGet(String urlStr,Map<String,String> headerMap,Map<String,String> paramsMap){
		ConstatFinalUtil.LOGGER.info("---methodGet---");
		StringBuilder sb=new StringBuilder();
		/*这里主要是将参数拼接起来*/
		if(paramsMap.size()>0){
			/*处理Url传参数*/
			if(paramsMap.size()>0){
				if(urlStr.indexOf("?")== -1){
					urlStr+="?";
				}else{
					urlStr+="&";
				}
			}
			for(Iterator<Entry<String,String>> iterator =paramsMap.entrySet().iterator();iterator.hasNext();){
				Entry<String,String> me=(Entry<String,String>)iterator.next();
				String key=me.getKey()+"";
				String val=me.getValue()+"";
				urlStr+=key+"="+val+"&";
			}
		}
		/*建立http链接，将头文件参数放到url中*/
		try {
			/*和上面同理，主要是将头文件信息放到头部信息中*/
			URL url=new URL(urlStr);
			HttpURLConnection connection=(HttpURLConnection)url.openConnection();
			for (Iterator<Entry<String,String>> iterator = headerMap.entrySet().iterator(); iterator.hasNext();) {
				Entry<String,String> me=(Entry<String,String>)iterator.next();
				String key=me.getKey()+"";
				String val=me.getValue()+"";
				connection.setRequestProperty(key, val);
			}
			ConstatFinalUtil.LOGGER.info("--状态码{}---",connection.getResponseCode());
			/**
			 * 输入流
			 * 只有状态是200的时候才可以通过connection.getInputStream();
			 * 其他只能通过connection.getErrorStream();
			 */
			InputStream is=null;
			if(connection.getResponseCode()==200){
				is=connection.getInputStream();
			}else{
				is=connection.getErrorStream();
			}
			BufferedReader br=new BufferedReader(new InputStreamReader(is, "UTF-8"));
			String line="";
			while((line=br.readLine())!=null){
				sb.append(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}
	/**
	 * 模拟POST请求
	 * @param urlStr
	 * @param headerMap
	 * @param paramsMap
	 * @return
	 */
	/*通过post方法来获取网页信息*/
	public String methodPost(String urlStr,Map<String,String> headerMap,Map<String,String> paramsMap){
		ConstatFinalUtil.LOGGER.info("---methodPost---");
		StringBuilder sb=new StringBuilder();
		try {
			/*创建一个URL*/
			URL url=new URL(urlStr);
			HttpURLConnection connection=(HttpURLConnection)url.openConnection();
			/*设置一下connection*/
			connection.setDoInput(true);
			connection.setDoOutput(true);
			
			/*放信封*/
			for (Iterator<Entry<String,String>> iterator = headerMap.entrySet().iterator(); iterator.hasNext();) {
				Entry<String,String> me=(Entry<String,String>)iterator.next();
				String key=me.getKey()+"";
				String val=me.getValue()+"";
				connection.setRequestProperty(key, val);
			}
			/*设置参数*/
			StringBuilder sbPara=new StringBuilder();
			for(Iterator<Entry<String,String>> iterator =paramsMap.entrySet().iterator();iterator.hasNext();){
				Entry<String,String> me=(Entry<String,String>)iterator.next();
				String key=me.getKey()+"";
				String val=me.getValue()+"";
				sbPara.append(key+"="+val+"&");
			}
			/*放置参数之后开始输出流*/
			OutputStream os=connection.getOutputStream();
			BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
			/*将参数写入到输入流中*/
			bw.write(sbPara.toString());
			bw.flush();
			bw.close();
			/*必须在输入流完成之后才能执行状态码操作*/
			ConstatFinalUtil.LOGGER.info("--状态码{}---",connection.getResponseCode());
			/*此时数据写入之后，用输入流读取数据*/
			InputStream is=null;
			if(connection.getResponseCode()==200){
				is=connection.getInputStream();
			}else{
				is=connection.getErrorStream();
			}
			/*也是将数据读取到String文件中*/
			BufferedReader br=new BufferedReader(new InputStreamReader(is, "UTF-8"));
			String line="";
			while((line=br.readLine())!=null){
				sb.append(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}
}
